﻿using Microsoft.AspNetCore.Mvc;
using WebAPI.Dao;
using WebAPI.Entities;
using WebAPI.Services;

namespace WebAPI.Controllers;

[ApiController]
[Route("api/[controller]/[Action]")]
public class TagController : ControllerBase
{
    private TagService _tagService = new TagService();
    
    [HttpPost(Name = "AddTag")]
    public string AddTag(Tag tag)
    {
        bool flag = _tagService.AddTag(tag);
        if (!flag)
        {
            return ResultData.Fail("创建标签失败，请稍后重试");
        }
        return ResultData.Ok("增添标签成功");
    }
    
    [HttpGet(Name = "GetTags")]
    public string GetTags()
    {
        List<Tag> tags = _tagService.GetAllTags();
        if (tags.Count == 0)
        {
            return ResultData.Fail("还未有标签，请稍后重试");
        }
        return ResultData.Ok(tags);
    }
    
    [HttpPut(Name = "DeleteTag")]
    public string DeleteTag(Tag tag)
    {
        bool flag = _tagService.DeleteTag(tag);
        if (!flag)
        {
            return ResultData.Fail("删除标签失败，请稍后重试");
        }
        return ResultData.Ok("删除成功");
    }
    
    [HttpPut(Name = "UpdateTag")]
    public string UpdateTag(Tag tag)
    {
        bool flag = _tagService.UpdateTag(tag);
        if (!flag)
        {
            return ResultData.Fail("更新标签失败，请稍后重试");
        }
        return ResultData.Ok("更新标签成功");
    }
}